<?php
require_once(substr(__FILE__, 0, strrpos(__FILE__, '/')) . "/inc/JSON.php");
require_once(substr(__FILE__, 0, strrpos(__FILE__, '/')) . "/inc/objects.php");

class Preferences
{
  function Preferences()
  {
    $this->path = substr(__FILE__, 0, strrpos(__FILE__, '/')) . "/";
    $this->storagePath = $this->path . "preferences.js";
    $this->username = exec("/usr/bin/id -un");
    if (!$this->username)
    {
      $error = new Error("Preferences: Could not get username in constructor!");
    }
    $this->username = trim($this->username);
    $this->xmltvFolder = "/Users/" . $this->username . "/Library/Xmltv/";
    $this->logosFolder = $this->xmltvFolder . "logos/";
    $this->schedulesFolder = $this->xmltvFolder . "schedules/";
    $this->channelsFolder = $this->xmltvFolder . "channels/";
    $this->reload();
  }
  
  function createStandardDb()
  {
    $db = new Object();
    $db->daysToDownload = 4; // yesterday, today, tomorrow, the day after tomorrow
    $db->lastSuccessfulRun = -1; // Has never had a successful run
    $db->vacationMode = false; // Vacation mode is turned off by default
    $db->channels = array();
    $db->lastClean = 0;
    return $db;
  }
  
  function save()
  {
    if (!isset($this->db) || !isset($this->db->daysToDownload))
    {
      return new Error("Preferences->save: Could not save file  $this->storagePath because this->db seems to be corrupt! What did you do?");
    }
    else
    {
      $db = new Object();
      $db->preferences = $this->db;
      $fp = fopen($this->storagePath, 'w+');
      if (!isset ($fp) || !$fp)
      {
        return new Error("Preferences->save: Was not allowed to write to $this->storagePath.\nIs it write protected?"); 
      }
      else
      {
        fwrite($fp, utf8_encode(json_encode($db)));
        fclose($fp);
        return true;
        //$output = new Output("Preferences->save: saved $this->storagePath");
      }
    }
  }
  
  function restoreDefaults()
  {
    $this->db = $this->createStandardDb();
    $this->save(); 
  }
  
  function reload()
  {
    $contents = @ file_get_contents($this->storagePath);
    if (!$contents)
    {
      $this->db = $this->createStandardDb();
      //$output = new Output("Preferences->reload: $this->storagePath found but was empty. Created a new standard db.");
    }
    else
    {
      $jsonObj = json_decode($contents);
      if (isset ($jsonObj->preferences))
      {
        //$output = new Output("Preferences->reload: Contents of $this->storagePath\n$contents");
        $this->db = $jsonObj->preferences;
      }
      else
      {
        $this->db = $this->createStandardDb();
        $error = new Error("Preferences->reload: File " . $this->storagePath . " was corrupt!");
      }
    }
  }
  
  function getChannels()
  {
    $this->reload();
    return $this->db->channels;
  }
  
  function getSchedulesFolder()
  {
    return $this->schedulesFolder;
  }
  
  function getChannelsFolder()
  {
    return $this->channelsFolder;
  }
  
  function getLogosFolder()
  {
    return $this->logosFolder;
  }
  
  function getLastClean()
  {
    $this->reload();
    return $this->db->lastClean;
  }
  
  function setLastClean($timestamp, $save = true)
  {
    $this->db->lastClean = $timestamp;
    if ($save)
    {
      $this->save();
    }
  }
}
?>